#!/usr/bin/env python3
"""
intltool MCP Server

面向 intltool 国际化工具套件的 MCP server，提供对 intltool 命令行操作的封装。

MCP tools 列表：
1. intltool_extract - 从各种文件类型中提取可翻译字符串
   参数: command_options (str) - 命令行选项和参数
   返回: 统一的命令执行结果 JSON

2. intltool_merge_basic - 基本文件合并功能，支持多种文件类型
   参数: command_options (str) - 命令行选项和参数  
   返回: 统一的命令执行结果 JSON

3. intltool_merge_advanced - 高级合并功能，支持 UTF8、缓存等选项
   参数: command_options (str) - 命令行选项和参数
   返回: 统一的命令执行结果 JSON

4. intltool_prepare - 准备国际化文件和环境
   参数: command_options (str) - 命令行选项和参数
   返回: 统一的命令执行结果 JSON

5. intltool_update - 更新翻译文件和生成报告
   参数: command_options (str) - 命令行选项和参数
   返回: 统一的命令执行结果 JSON

6. intltoolize - 初始化项目国际化配置
   参数: command_options (str) - 命令行选项和参数
   返回: 统一的命令执行结果 JSON

所有工具返回统一的 JSON 结构：
{
  "success": bool,      # 命令是否成功执行（exit_code == 0）
  "command": str,       # 实际执行的完整命令
  "exit_code": int,     # 命令的退出码
  "stdout": str,        # 标准输出内容
  "stderr": str         # 标准错误内容
}
"""

import subprocess
import shlex
from mcp.server.fastmcp import FastMCP, Context

# 创建 MCP 服务器实例
mcp = FastMCP("Intltool MCP Server")

@mcp.tool()
def intltool_extract(command_options: str = "") -> dict:
    """
    执行 intltool-extract 命令，从各种文件类型中提取可翻译字符串
    
    参数:
        command_options (str): 命令行选项和参数，如 "--type=gettext/xml file.xml"
        
    返回:
        dict: 统一的命令执行结果，包含 success, command, exit_code, stdout, stderr
    """
    cmd = f"intltool-extract {command_options}".strip()
    try:
        result = subprocess.run(shlex.split(cmd), capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": cmd,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Error executing command: {str(e)}"
        }

@mcp.tool()
def intltool_merge_basic(command_options: str = "") -> dict:
    """
    执行 intltool-merge 基本合并功能，支持桌面文件、键文件等基本类型
    
    参数:
        command_options (str): 命令行选项和参数，如 "-d po input.desktop output.desktop"
        
    返回:
        dict: 统一的命令执行结果，包含 success, command, exit_code, stdout, stderr
    """
    cmd = f"intltool-merge {command_options}".strip()
    try:
        result = subprocess.run(shlex.split(cmd), capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": cmd,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Error executing command: {str(e)}"
        }

@mcp.tool()
def intltool_merge_advanced(command_options: str = "") -> dict:
    """
    执行 intltool-merge 高级合并功能，支持 UTF8、缓存、透传等高级选项
    
    参数:
        command_options (str): 命令行选项和参数，如 "-u -r po input.rfc822deb output.rfc822deb"
        
    返回:
        dict: 统一的命令执行结果，包含 success, command, exit_code, stdout, stderr
    """
    cmd = f"intltool-merge {command_options}".strip()
    try:
        result = subprocess.run(shlex.split(cmd), capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": cmd,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Error executing command: {str(e)}"
        }

@mcp.tool()
def intltool_prepare(command_options: str = "") -> dict:
    """
    执行 intltool-prepare 命令，准备国际化文件和环境
    
    参数:
        command_options (str): 命令行选项和参数，如 "-x" 或 "--verbose"
        
    返回:
        dict: 统一的命令执行结果，包含 success, command, exit_code, stdout, stderr
    """
    cmd = f"intltool-prepare {command_options}".strip()
    try:
        result = subprocess.run(shlex.split(cmd), capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": cmd,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Error executing command: {str(e)}"
        }

@mcp.tool()
def intltool_update(command_options: str = "") -> dict:
    """
    执行 intltool-update 命令，更新翻译文件和生成报告
    
    参数:
        command_options (str): 命令行选项和参数，如 "-p" 或 "-s" 等
        
    返回:
        dict: 统一的命令执行结果，包含 success, command, exit_code, stdout, stderr
    """
    cmd = f"intltool-update {command_options}".strip()
    try:
        result = subprocess.run(shlex.split(cmd), capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": cmd,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Error executing command: {str(e)}"
        }

@mcp.tool()
def intltoolize(command_options: str = "") -> dict:
    """
    执行 intltoolize 命令，初始化项目国际化配置
    
    参数:
        command_options (str): 命令行选项和参数，如 "--automake" 或 "-c"
        
    返回:
        dict: 统一的命令执行结果，包含 success, command, exit_code, stdout, stderr
    """
    cmd = f"intltoolize {command_options}".strip()
    try:
        result = subprocess.run(shlex.split(cmd), capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": cmd,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": cmd,
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Error executing command: {str(e)}"
        }

if __name__ == "__main__":
    mcp.run()